﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace GtalkBot
{
    class Tools
    {


        public static string getac(string a)
        {

            return doget("http://lucky.anquanbao.com/q.php?q=" + a);
           
        }

        public static string doget(string a)
        {
            try
            {
                WebRequest req = WebRequest.Create(a);
                HttpWebRequest httpreg = (HttpWebRequest)req;
                httpreg.Method = "GET";
                httpreg.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; CIBA)";
                WebResponse resp = httpreg.GetResponse();
                System.IO.StreamReader reader = new System.IO.StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                return reader.ReadToEnd();
            }
            catch
            {

                return "";
            }
        }



        public static string AtoS(string asc)
        {
            string[] xx = StringSplit(asc, "\\u");
            string rt = "";
            for (int i = 1; i < xx.Length; i++)
            {
                rt += ((char)(short)Convert.ToInt32(xx[i].Substring(0, 4), 16)).ToString();
            }
            return rt;
        }



        // <summary>
        /// 将字符串分割成数组 
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <returns></returns>
        public static string[] StringSplit(string strSource, string strSplit)
        {
            string[] strtmp = new string[1];
            int index = strSource.IndexOf(strSplit, 0);
            if (index < 0)
            {
                strtmp[0] = strSource;
                return strtmp;
            }
            else
            {
                strtmp[0] = strSource.Substring(0, index);
                return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
            }
        }
        /// <summary>
        /// 采用递归将字符串分割成数组
        /// </summary>
        /// <param name="strSource"></param>
        /// <param name="strSplit"></param>
        /// <param name="attachArray"></param>
        /// <returns></returns>
        private static string[] StringSplit(string strSource, string strSplit, string[] attachArray)
        {
            string[] strtmp = new string[attachArray.Length + 1];
            attachArray.CopyTo(strtmp, 0);
            int index = strSource.IndexOf(strSplit, 0);
            if (index < 0)
            {
                strtmp[attachArray.Length] = strSource;
                return strtmp;
            }
            else
            {
                strtmp[attachArray.Length] = strSource.Substring(0, index);
                return StringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
            }
        }

    }
}
